package com.hui.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hui.entity.dao.SystemStore;
import com.hui.entity.enums.ShopCommonEnum;
import com.hui.entity.vo.SystemStoreQueryVo;
import com.hui.mapper.SystemStoreMapper;
import com.hui.service.SystemStoreService;
import com.hui.util.BeanCopyUtils;
import com.hui.util.module.location.LocationUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 门店自提(SystemStore)表服务实现类
 *
 * @author hui
 * @since 2022-09-14 15:12:51
 */
@Service("systemStoreService")
@RequiredArgsConstructor
public class SystemStoreServiceImpl extends ServiceImpl<SystemStoreMapper, SystemStore> implements SystemStoreService {
    private final SystemStoreMapper systemStoreMapper;

    @Override
    public SystemStoreQueryVo getStoreInfo(String latitude, String longitude) {
        //门店地址只返回第一个
        LambdaQueryWrapper<SystemStore> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemStore::getIsShow, ShopCommonEnum.SHOW_1.getValue()).orderByDesc(SystemStore::getId).last("limit 1");
        SystemStore systemStore = this.systemStoreMapper.selectOne(wrapper);
        if (systemStore == null) {
            return null;
        } else {
//            String mention = (String) RedisUtil.get(ShopKeyUtils.getStoreSelfMention());
            // if (!StrUtil.isBlank(mention) && !ShopCommonEnum.ENABLE_2.getValue().toString().equals(mention)) {//门店存在且未关闭，原判断
            if (true) { //TODO 暂时放行
                SystemStoreQueryVo systemStoreQueryVo = BeanCopyUtils.copyBean(systemStore, SystemStoreQueryVo.class);
                if (StrUtil.isNotEmpty(latitude) && StrUtil.isNotEmpty(longitude)) {
                    /*
                     计算距离
                     LocationUtils 使用了工具类，没搞明白，在上一级的调用者，传入的 latitude longitude 为null ,不会进入到这里来
                     */
                    double distance = LocationUtils.getDistance(Double.parseDouble(latitude), Double.parseDouble(longitude), Double.parseDouble(systemStore.getLatitude()), Double.parseDouble(systemStore.getLongitude()));
                    systemStoreQueryVo.setDistance(String.valueOf(distance));
                }

                return systemStoreQueryVo;
            } else {
                return null;
            }
        }
    }
}

